package pl.edu.agh.io.gpstracker.server.service.handler;

import net.customware.gwt.dispatch.server.ActionHandlerRegistry;
import net.customware.gwt.dispatch.server.ExecutionContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import pl.edu.agh.io.gpstracker.client.action.LoginAction;
import pl.edu.agh.io.gpstracker.client.dto.UserDto;
import pl.edu.agh.io.gpstracker.client.result.ObjectResult;
import pl.edu.agh.io.gpstracker.service.UserService;

import com.adeoservices.gwt.dispatch.spring.server.handler.SpringActionHandler;

/**
 * 
 * @author wkedzior
 * 
 */
@Component
public class LoginHandler extends
		SpringActionHandler<LoginAction, ObjectResult<UserDto>> {

	@Autowired
	private UserService userService;
	
	@Autowired
	public LoginHandler(ActionHandlerRegistry actionHandlerRegistry) {
		super(actionHandlerRegistry);

	}

	@Override
	public Class<LoginAction> getActionType() {
		return LoginAction.class;
	}

	@Override
	public ObjectResult<UserDto> execute(LoginAction action,
			ExecutionContext context) {
		return new ObjectResult<UserDto>(userService.login(action.getLogin(), action.getPassword()));
	}

	@Override
	public void rollback(LoginAction action, ObjectResult<UserDto> result,
			ExecutionContext context) {
	}
}
